local function BuffAttack(inst, data)
	local bufflevel = inst.components.musha_equipbuff.bufflevel
	if not data.target.components.health:IsDead() then
		if inst.components.musha_equipbuff.buff == "fire" then
			data.target.components.health:DoDelta(math.random(40, 65) * bufflevel)
		elseif inst.components.musha_equipbuff.buff == "ice" then
			if data.target.freezable then
				data.target.components.freezable:AddColdness(0.6 * bufflevel)
			end
		elseif inst.components.musha_equipbuff.buff == "phoenix" then
			data.target.components.health:DoDelta(math.random(15, 35) * bufflevel)
			if math.random() < 0.6 * bufflevel then
				inst.components.health:DoDelta(math.random(5, 15) * bufflevel)
				local fx3 = SpawnPrefab("groundpoundring_fx")
				fx3.Transform:SetScale(0.3, 0.3, 0.3)
				fx3.Transform:SetPosition(data.target:GetWorldPosition():Get())
			end
		end
	end
end

local function BuffBeAttack(inst, data)
	local bufflevel = inst.components.musha_equipbuff.bufflevel
	if not data.attacker.components.health:IsDead() then
		if inst.components.musha_equipbuff.buff == "fire" then
			data.attacker.components.health:DoDelta(math.random(15, 25) * bufflevel)
		elseif inst.components.musha_equipbuff.buff == "ice" then
			if data.attacker.freezable then
				data.attacker.components.freezable:AddColdness(0.4 * bufflevel)
				local fx = SpawnPrefab("icespike_fx_" .. math.random(1, 4))
				fx.Transform:SetScale(1, 0.4 + 0.4 * bufflevel, 1)
				fx.Transform:SetPosition(data.attacker:GetPosition():Get())
			end
		elseif inst.components.musha_equipbuff.buff == "phoenix" then
			if math.random() < 0.4 * bufflevel and data.attacker.components.sleeper then
				data.attacker.components.sleeper:AddSleepiness(5 * bufflevel, 25 * bufflevel)
				SpawnPrefab("small_puff").Transform:SetPosition(data.attacker:GetPosition():Get())
			end
		end
	end
end

local function Equip_Change(inst)
	local self = inst.components.musha_equipbuff
	inst:RemoveEventCallback("onhitother", BuffAttack)
	inst:RemoveEventCallback("attacked", BuffBeAttack)
	local hands_equip = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	local body_equip = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
	local head_equip = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
	if
		not (hands_equip and body_equip and head_equip) or
			not (hands_equip.components.musha_equipment and body_equip.components.musha_equipment and
				head_equip.components.musha_equipment)
	 then
		return
	end
	if
		(hands_equip.type == "fire" or hands_equip.type == "fireice") and body_equip.type == "fire" and
			head_equip.type == "fire"
	 then
		self:Buff("fire")
	elseif
		(hands_equip.type == "ice" or hands_equip.type == "fireice") and body_equip.type == "ice" and head_equip.type == "ice"
	 then
		self:Buff("ice")
	elseif hands_equip.type == "phoenix" and body_equip.type == "phoenix" and head_equip.type == "phoenix" then
		self:Buff("phoenix")
	else
		self:Buff("none")
	end
	if self.buff then
		self:BuffLevel(
			hands_equip.components.musha_equipment.currentlevel,
			body_equip.components.musha_equipment.currentlevel,
			head_equip.components.musha_equipment.currentlevel
		)
	end
end

local Musha_Equip =
	Class(
	function(self, inst)
		self.inst = inst
		self.buff = nil
		self.building = {
			["musha_armor_princess"] = false,
			["musha_armor_forest"] = false,
			["musha_armor_shadow"] = false,
			["musha_sword_fire"] = false,
			["musha_sword_frost"] = false,
			["musha_phoenixaxe"] = false,
			["musha_frosthammer"] = false,
			["musha_bowm"] = false,
			["musha_ironcathat"] = false,
			["musha_ironbunnyhat"] = false
		}
		self.costing = {
			["musha_sword_base"] = 0,
			["musha_sword_fire"] = 0,
			["musha_sword_frost"] = 0,
			["musha_bunnyhat"] = 0
		}
		self.inst:ListenForEvent("equip", Equip_Change)
		self.inst:ListenForEvent("unequip", Equip_Change)
		self.inst:ListenForEvent(
			"makerecipe",
			function(inst, data)
				if self.inst.components.builder.freebuildmode then
					return
				end
				if self.building[data.recipe.product] ~= nil then
					self.building[data.recipe.product] = true
					self.resettask =
						inst:DoTaskInTime(
						1.5,
						function()
							self.building[data.recipe.product] = false
							self.resettask:Cancel()
							self.resettask = nil
						end
					)
				end
			end
		)
		self.inst:ListenForEvent(
			"builditem",
			function(inst, data)
				if self.inst.components.builder.freebuildmode then
					return
				end
				if self.building[data.recipe.product] then
					local exp, i = 0, 0
					for k, v in pairs(data.recipe.ingredients) do
						if self.costing[v.type] then
							exp = exp + self.costing[v.type]
							self.costing[v.type] = 0
							i = i + 1
						end
					end
					data.item.exp = exp / i
				end
			end
		)
	end
)

function Musha_Equip:Buff(buff)
	if buff == "none" then
		self.buff = nil
		return
	end
	local inst = self.inst
	self.buff = buff
	if buff == "fire" or "ice" or "phoenix" then
		inst:ListenForEvent("onhitother", BuffAttack)
		inst:ListenForEvent("attacked", BuffBeAttack)
	end
end

function Musha_Equip:BuffLevel(level_hands, level_body, level_head)
	self.bufflevel = math.max(level_hands * level_body * level_head / 2700, 0.3)
end

return Musha_Equip
